<?php

namespace Biz\model;

use Doraemon\pockets\datebase\BaseDbModel;

class ShopInviteCode extends BaseDbModel
{
    const _CONFIG_FILE_ = 'db_shop';

    const _CONFIG_SELECT_ = 'db_shop_info';

    const _DATABASE_ = 'shop_info';

    const _TABLE_NAME = 'shop_invite_code';

    const _SUB_TABLE = false;

    const STATUS_DISABLED = 0;//失效状态

    const STATUS_NORMAL = 1;//正常状态

    const TYPE_NORMAL = 1;//有用户uid的邀请码

    const TYPE_NOUID = 0;//没有uid的邀请码

    public static function getInfoById($id)
    {
        return self::get('*', [
            'status' => self::STATUS_NORMAL,
            'id' => $id
        ]);
    }

    public static function getInfoByUid($uid)
    {
        return self::get('*', [
            'status' => self::STATUS_NORMAL,
            'uid' => $uid
        ]);
    }

    public static function checkCodeByUid($uid, $code)
    {
        $info = self::get('*', [
            'status' => self::STATUS_NORMAL,
            'uid' => $uid,
            'code' => $code
        ]);
        return $info ? true : false;
    }

    public static function getByCode($code)
    {
        return self::get('*', [
            'code' => $code
        ]);
    }

    public static function addInviteCode($data)
    {
        self::insert([
            "type" => $data['type'],
            "uid" => $data['uid'],
            "code" => $data['code'],
            "status" => $data['status'],
            'invalid_date' => $data['invalid_date'],
        ]);
        return self::id();
    }

    public static function updateInfo($data, $where)
    {
        $result = self::update($data, $where);
        return $result->rowCount();
    }
}